package com.leyou.item.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import com.leyou.common.vo.PageResult;
import com.leyou.item.mapper.BrandMapper;
import com.leyou.item.pojo.Brand;

import com.sun.org.apache.regexp.internal.RE;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;


@Service
public class BrandService {

    @Autowired
    private BrandMapper brandMapper;

    public PageResult<Brand> queryBrandById(
            Integer page, Integer rows, String sortBy, Boolean desc, String key) {
        // 开始分页
        PageHelper.startPage(page, rows);
        // 过滤
        Example example = new Example(Brand.class);
        if (StringUtils.isNotBlank(key)) {
            example.createCriteria().orLike("name", "%" + key + "%")
                    .orEqualTo("letter", key.toUpperCase());
        }
        if (StringUtils.isNotBlank(sortBy)) {
            // 排序
            String orderByClause = sortBy + (desc ? " DESC" : " ASC");
            example.setOrderByClause(orderByClause);
        }
        // 查询
        Page<Brand> pageInfo = (Page<Brand>) brandMapper.selectByExample(example);
        // 返回结果
        return new PageResult<>(pageInfo.getTotal(), pageInfo);
    }
}





























//@Service
//public class BrandService {
//    @Autowired
//    private BrandMapper brandMapper;
//
//
//    public PageResult<Brand> queryBrandByPage(Integer page, Integer rows, String sortBy, Boolean desc, String key) {
//
//        //分页
//        PageHelper.startPage(page, rows);
//        //过滤
//        Example example = new Example(Brand.class);
//        if (StringUtils.isNotBlank(key)) {
//            //过滤条件
//            example.createCriteria().orLike("name", "%" + key + "%")
//                    .orEqualTo("letter", key.toUpperCase());
//        }
//
//        //排序
//        if (StringUtils.isNotBlank(sortBy)) {
//            String orderByClause = sortBy + (desc ? " DESC" : "ASC");
//            example.setOrderByClause(orderByClause);
//        }
//        //查询
//        List<Brand> list = brandMapper.selectByExample(example);
//        if (CollectionUtils.isEmpty(list)) {
//            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
//
//        }
//        //解析分页结果
//        PageInfo<Brand> Info = new PageInfo<>(list);
//        return new PageResult<>(Info.getTotal(), list);
//
//
//    }
//}
